package com.shijiu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shijiu.dao.mapper.OrderMapper;
import com.shijiu.pojo.*;
import com.shijiu.service.FoodService;
import com.shijiu.service.OrderFoodService;
import com.shijiu.service.OrderService;
import com.shijiu.vo.OrderFoodVo;
import com.shijiu.vo.OrderListVo;
import com.shijiu.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderFoodService orderFoodService;
    @Autowired
    private FoodService foodService;
    @Override
    public Result selectOrderList(Integer shopId) {
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.select(Order::getId,
                    Order::getOpenId,
                    Order::getName,
                    Order::getAddress,
                    Order::getPhone,
                    Order::getStatus,
                    Order::getCreated,
                    Order::getUpdated)
                .eq(Order::getShopId,shopId)
                .eq(Order::getAdminDelete,false)
                .orderByDesc(Order::getCreated);
        List<Order> orders = orderMapper.selectList(qw);
        log.info("OrderServiceImpl sout orders {}",orders);
        return Result.success(orders);
    }

    public Result updateStatus(Order order){
        orderMapper.updateById(order);
        return Result.success("ok");
    }

    @Override
    public Result showFood(Integer id) {
        LambdaQueryWrapper<OrderFood> qw = new LambdaQueryWrapper<>();
        qw.eq(OrderFood::getOrderId,id);
        //根据订单id在orderfood表中查询出对应的所有菜品包括(foodId,foodCount)
        List<OrderFood> orderFoodList = orderFoodService.list(qw);
        //最终展示的菜品列表
        List<OrderFoodVo> result = new ArrayList<>();
        for (OrderFood orderFood : orderFoodList) {
            //在food表中查询出来完整的food对象
            Food food = foodService.getById(orderFood.getFoodId());
            OrderFoodVo orderFoodVo = new OrderFoodVo();
            //拷贝基本属性
            BeanUtils.copyProperties(food,orderFoodVo);
            //设置菜品数量
            orderFoodVo.setCount(orderFood.getCount());
            result.add(orderFoodVo);
        }
        return Result.success(result);
    }

    @Override
    public Result selectOrderListByPage(Integer shopId, Map<String, Object> requestBodyMaps) {
        Integer pageCount = (Integer) requestBodyMaps.get("page");
        Integer pageSize = (Integer) requestBodyMaps.get("pageSize");
        String name = (String) requestBodyMaps.get("name");
        String phone = (String) requestBodyMaps.get("phone");
        String address = (String) requestBodyMaps.get("address");
        LambdaQueryWrapper<Order> queryWrap = new LambdaQueryWrapper<>();
        //如果姓名不为 null 不为空，模糊查询
        if (StrUtil.isNotBlank(name)) {
            queryWrap.like(Order::getName, "%" + name + "%");
        }
        //如果手机号不为空，查询手机号
        if (StrUtil.isNotBlank(phone)) {
            queryWrap.eq(Order::getPhone, phone);
        }
        //如果地址信息不为空，查询地址
        if (StrUtil.isNotBlank(address)) {
            queryWrap.like(Order::getAddress, address);
        }
        queryWrap.select(Order::getId,
                        Order::getOpenId,
                        Order::getName,
                        Order::getAddress,
                        Order::getPhone,
                        Order::getStatus,
                        Order::getCreated,
                        Order::getUpdated)
                .eq(Order::getShopId,shopId)
                .eq(Order::getAdminDelete,false)
                .orderByDesc(Order::getCreated);
        List<Order> orders = orderMapper.selectList(queryWrap);
        // 查询第pageCount页，每页pageSize条记录
        Page<Order> page = new Page<>(pageCount, pageSize);
        IPage<Order> orderPage = orderMapper.selectPage(page, queryWrap);
        // 当前页的记录列表
        List<Order> orderList = orderPage.getRecords();
        //总记录条数
        long total = page.getTotal();
        OrderListVo orderListVo = new OrderListVo();
        orderListVo.setOrderVoList(orderList);
        orderListVo.setTotal(total);
        log.info("OrderServiceImpl sout orders {}",orderListVo);
        return Result.success(orderListVo);
    }
}
